package com.snline.crm.common;


public class CookieUtils {

    public static final String USER_COOKIE = "user.cookie";  
    
/*    // 添加一个cookie  
    public Cookie addCookie(User user) {  
        Cookie cookie = new Cookie(USER_COOKIE, user.getUsername() + ","  
                + user.getPassword());  
        System.out.println("添加cookie");  
        cookie.setMaxAge(60 * 60 * 24 * 14);// cookie保存两周  
        return cookie;  
    }  
  
    // 得到cookie  
    public boolean getCookie(HttpServletRequest request, UserDao userDAO) {  
        Cookie[] cookies = request.getCookies();  
        System.out.println("cookies: " + cookies);  
        if (cookies != null) {  
            for (Cookie cookie : cookies) {  
                System.out.println("cookie: " + cookie.getName());  
                if (CookieUtils.USER_COOKIE.equals(cookie.getName())) {  
                    String value = cookie.getValue();  
                    if (StringUtils.isNotBlank(value)) {  
                        String[] split = value.split(",");  
                        String username = split[0];  
                        String password = split[1];  
                        User user = userDAO.checkUser(username, password);  
                        if (user != null) {  
                            HttpSession session = request.getSession();  
                            session.setAttribute(LoginAction.USER_SESSION, user);// 添加用户到session中  
                            return true;  
                        }  
                    }  
                }  
            }  
        }  
        return false;  
    }  
  
    // 删除cookie  
    public Cookie delCookie(HttpServletRequest request) {  
        Cookie[] cookies = request.getCookies();  
        if (cookies != null) {  
            for (Cookie cookie : cookies) {  
                if (USER_COOKIE.equals(cookie.getName())) {  
                    cookie.setValue("");  
                    cookie.setMaxAge(0);  
                    return cookie;  
                }  
            }  
        }  
        return null;  
    }  */
}
